<template>
  <div class="container">
    <div class="login-wrapper">
      <div class="header">Login</div>
      <form @submit.prevent="login">
        <div class="form-wrapper">
          <input type="text" name="username" v-model="username" placeholder="Username" class="input-item"  @input="clearErrorMsg">
          <input type="password" name="password" v-model="password" placeholder="Password" class="input-item"  @input="clearErrorMsg">
           <transition name="fade">
            <span v-if="errorMsg" class="error">{{ errorMsg }}</span>
          </transition>
          <button type="submit" class="btn">登录</button>
        </div>
        <div class="msg">
          <span >Don't have an account? <span class="sign" @click="goToSignUp">Sign up</span></span>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup lang="ts"> 
import { ref } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
const username = ref('')
const password = ref('')
const errorMsg = ref('')
function login() {
  // 前端校验用户名和密码
  if (username.value === 'millie' && password.value === '123456') {
    // 登录成功，跳转到其他页面
    if(errorMsg) {
      errorMsg.value=''
    }
    localStorage.setItem('loggedIn', 'true')
    router.push({name:'home'})
  } else {
    // 登录失败，显示错误信息
    if(username.value === '' && password.value === '')
    errorMsg.value = '请输入用户名和密码'
  }
}
function clearErrorMsg() {
  // 清除错误提示信息
  errorMsg.value = ''
}
function goToSignUp() {
  // 跳转到注册页面
  router.push({ name: 'register' })
}
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}

html {
  height: 100%;
}

body {
  height: 100%;
}

.container {
  height: 100vh;
  background-image: linear-gradient(to right, #fbc2eb, #a6c1ee);
}

.login-wrapper {
  background-color: #fff;
  width: 358px;
  height: 588px;
  border-radius: 15px;
  padding: 0 50px;
  position: relative;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.header {
  font-size: 38px;
  font-weight: bold;
  text-align: center;
  line-height: 200px;
}

.input-item {
  display: block;
  width: 100%;
  margin-bottom: 20px;
  border: 0;
  padding: 10px;
  border-bottom: 1px solid rgb(128, 125, 125);
  font-size: 15px;
  outline: none;
}

.input-item::placeholder {
  text-transform: uppercase;
}

.btn {
  text-align: center;
  padding: 10px;
  width: 100%;
  margin-top: 40px;
  background-image: linear-gradient(to right, #a6c1ee, #fbc2eb);
  color: #fff;
}

.msg {
  margin-top: 20px;
  text-align: center;
  line-height: 20px;
}

.sign {
  color: #abc1ee;
}

.error {
  font-size: 12px;
  color: red;
}
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
</style>